<?php
namespace app\middleware;
use app\controller\Base;

/**
 * Rsa中间件
 * Description: 用于控制接口权限
 */

class Rsa extends Base
{
    /**
     * 解密Rsa中间件
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 获取加密数据
        $encryptedData = $request->post('data');
        // 私钥
$private_key_pem = '-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDVZaUAz4L25lYD
9oXlJwUuyR6mk2CPBgMQ+OSGR8WvhR74MTG/+tUqOPHesq6nZGTYrZubxNBUv3Ja
D4QsXyQtO2l/3tfZQQItRBw5pvONfXsT2/Ie1jMXS1adGCI56w99ZDnPru+f4Vom
sQXAP7VtHzcF+Lw5PomNDMSSJDZYNAj4mXhwovuXpmvSN2cZ14BjV3rps5R9wzgT
s5G8B8ZSxvSQyShWzrbMs+lZT5zvKG41AtTuBwZx6yxnyBiPJsLIkgmBExd7/Mi9
TXOFZinaSUr0mZmKKe1p5b5Rq2GcDPdApzDkxRvNisZEtxWoRlomlurQp/jevnFP
BAOXapGjAgMBAAECggEAeOCZxcrF0ZuJz7beA37F5cmQ7iR/RqTOKdgCgrm3FgRf
FZGwL6IjC0a0Y0RtwG4S3dYBVgv3QnoriS5O3V+LO3/y87bEJ9E2LXPuVQ3n2hbh
F8+355XH3QMU7emUwaAP5yRVxvm9zJ+3FEGkE1FDMKvE3EIYRIjTG63Gz01p5y2R
YlFS1kEAbP8T9nKAX/O30y6p/Poioc8ev8Dm8In3VSvDD9EcYFaHCZayMbGfefFw
hLNKvumyGAZxHxp3/CAzs5ExH/25zkUpFs7l/NjAV56ThkVsF81O0OmYHTL2xFBr
dthGaJeqCjdYpySoGCxOa8nr89qFOK3zMD56o1JgAQKBgQD1y7vRkcMGE2e2opix
iDWdjboP+33v9eF6p6b2UlBTzhF4PTwmL2/eHOR3TbP1AXGUrVqYXmDJodL9nFzW
i9YLMFbE8jdEbr+6KjCwj894KKNAuzSMiir/Wah4RapmwYnOABDCVDmJ+QzMX2A9
YoOzytL/Lou73fOprKfZQzXhowKBgQDeQZVaaSXE+ALtOeRMRxfJ1k7be4V69k1a
303tlSQtZ+INiAVwUG/SYEmdzfyXf5e7WF2h85cIJmv2EkAkvEAz09ymZauBbkWz
uFuSWenS82VDzoqqebGNeD+xpYIUfa8f6Am3pjgDb3vaZD58jNBco75Li1Yi9J6X
WN1j5COQAQKBgFF+kDJG0tOT4YIzXM8X6zbmw7cC2lmSO4UwSvJGjm2K2E32d5ft
iHirObC4qZ7lMKRr6aq1OAACUHNXwYw85uXkzBAWgu6EjbsKvAP+Wnprdt1CdITY
VrM9JddiucHCD4+mg+f6hu1RDsZAJSeVHzegWt7Sru/GAkDShwBJAXdjAoGBAMfW
8mKzrok3cvI4noTTW5lY/9irbO6N7inqJRidhETrZj9CcZl2ScVAXJDueNMDn7uN
DcujycZDPcmoyVH+jj/LnFdljVs4eqXI+1VRUUQjI4dffCEigLoySBsENT0VcVRf
//TIGQ9Fu3g3sFOvDV5uwJkOdOnDR6HP/mhl5nABAoGAYXv2j0zwJWoHITN3aw9E
1YtzzPJVG59G1A+jnuzpRnNQEzObiAq/4mllVvBnFkWvPMQBs7RU5wse6NBDJJG3
tXIs3kvcwEIMSq37WVzJlTuPE2qbDmHtzJdk3KknWG/Cfv4TFlMGIYs5P3NtlHFs
OhQRuCMobeHQMZBddU2UBVA=
-----END PRIVATE KEY-----';

        // 加载私钥
        $private_key = openssl_pkey_get_private($private_key_pem);

// // 从文件中加载公钥
// $public_key_pem = '-----BEGIN PUBLIC KEY-----
// MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDVYWXTv5KWjW1xDELJPi9guXWk
// aoO2TN63zIIak5FSeFHifuceiYctBqkN2ph9QbdE50qTxIWVATTA7wmFRnBLcxqu
// 7ApM0PnrCwleYnpAUNRlyXG4twX6BJHqN7oMwVEusjzVKBztfNHih34/puW1yuSK
// X3+l1i3j3EvoyMA/EQIDAQAB
// -----END PUBLIC KEY-----';
// $public_key = openssl_pkey_get_public($public_key_pem);
// // 明文
// $plaintext = '{"username":"test","password":"123456"}';

// 使用公钥加密明文
// openssl_public_encrypt($plaintext, $encrypted, $public_key);
        // 使用私钥解密密文
        openssl_private_decrypt(base64_decode($encryptedData), $decrypted, $private_key);
        // 定义一个存储解密后的变量,用于控制器读取
        // halt(json_decode($decrypted, true));
        $request->rsa_data = json_decode($decrypted, true);
        return $next($request);
    }
}